package org.dromara.bean.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.bean.constant.CommonCode;
import org.dromara.bean.constant.CommonConstant;
import org.dromara.bean.constant.OrderTypeConstant;
import org.dromara.bean.domain.HisGoodsOrder;
import org.dromara.bean.domain.HisMemberRefund;
import org.dromara.bean.domain.bo.HisGoodsOrderBo;
import org.dromara.bean.domain.vo.HisGoodsOrderVo;
import org.dromara.bean.manage.MemberRefundManage;
import org.dromara.bean.mapper.BaseMemberRefundMapper;
import org.dromara.bean.mapper.HisGoodsOrderMapper;
import org.dromara.bean.mapper.HisRegistrationMapper;
import org.dromara.bean.model.HisMemberRefundModel;
import org.dromara.bean.service.IBaseMemberRefundService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * @Description: 患者退费管理
 * @Author: jeecg-boot
 * @Date: 2020-08-18
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class BaseMemberRefundServiceImpl extends ServiceImpl<BaseMemberRefundMapper, HisMemberRefund> implements IBaseMemberRefundService {


    private final BaseMemberRefundMapper baseMemberRefundMapper;


    private final MemberRefundManage memberRefundManage;
    private final HisGoodsOrderMapper hisGoodsOrderMapper;

    private final HisRegistrationMapper registrationMapper;

    @Override
    public IPage<HisMemberRefundModel> pageListByKeyWord(Page<HisMemberRefundModel> page, String memberName, String doctorName, String checkStatus, String orderType) {
        IPage<HisMemberRefundModel> pageList = baseMemberRefundMapper.pageListByKeyWord(page, memberName, doctorName, checkStatus, orderType);
        for (HisMemberRefundModel memberRefundModel : pageList.getRecords()) {
            fillOrderNumber(memberRefundModel);
        }
        return pageList;
    }


    @Override
    public HisMemberRefund getByRegistrantId(String id) {
        return getOne(new LambdaQueryWrapper<HisMemberRefund>()
                .eq(HisMemberRefund::getOrderTreatId, id)
                .eq(HisMemberRefund::getOrderType, OrderTypeConstant.ORDER_REGISTRATION_TREAT), false);
    }

    @Override
    public HisMemberRefund getByGoodsOrderId(String id) {
        return getOne(new LambdaQueryWrapper<HisMemberRefund>()
            .eq(HisMemberRefund::getOrderTreatId, id)
            .eq(HisMemberRefund::getOrderType, OrderTypeConstant.ORDER_COMMODITY_TREAT), false);
    }

    @Override
    public TableDataInfo<?> queryPageList(Integer pageNo, Integer pageSize, String memberName, String doctorName, String checkStatus, String orderType) {
        Page<HisMemberRefundModel> page = new Page<>(pageNo, pageSize);
        IPage<HisMemberRefundModel> pageList = this.pageListByKeyWord(page, memberName, doctorName, checkStatus, orderType);
        return TableDataInfo.build(pageList);
    }


    @Override
    public R<?> queryById(String id) {
        HisMemberRefundModel hisMemberRefund = baseMapper.findById(id);
        fillOrderNumber(hisMemberRefund);
        return R.ok(hisMemberRefund);
    }


    @Override
    public R<?> audit(String checkStatus, String id, String cause) {
        HisMemberRefund hisMemberRefund = this.getById(id);
        if (hisMemberRefund == null) {
            return R.errorCode(CommonCode.ENTITY_NULL_ERROR);
        }
        R<?> result = new R<>();
        switch (hisMemberRefund.getOrderType()) {
            case OrderTypeConstant.ORDER_REGISTRATION_TREAT:
                result = memberRefundManage.audit4Registration(hisMemberRefund, checkStatus, cause);
                break;
            case OrderTypeConstant.ORDER_COMMODITY_TREAT:
                result = memberRefundManage.audit4Commodity(hisMemberRefund, checkStatus, cause);
                break;
            default:
                result.setMsg("不支持的订单类型");
                break;
        }
        return result;
    }

    /**
     * 查询符合条件的商城订单列表
     *
     * @param bo 查询条件
     * @return 商城订单列表
     */
    @Override
    public List<HisMemberRefund> queryList(HisMemberRefund bo) {
        LambdaQueryWrapper<HisMemberRefund> lqw = buildQueryWrapper(bo);
        List<HisMemberRefund> hisMemberRefunds = baseMapper.selectList(lqw);
        hisMemberRefunds.forEach(r -> {
            fillOrderNumber(r);
        });

        return hisMemberRefunds;

    }


    private LambdaQueryWrapper<HisMemberRefund> buildQueryWrapper(HisMemberRefund bo) {
        LambdaQueryWrapper<HisMemberRefund> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMemberId() != null, HisMemberRefund::getMemberId, bo.getMemberId());
        lqw.eq(ObjectUtil.isNotEmpty(bo.getStatus()), HisMemberRefund::getStatus, bo.getStatus());
        lqw.eq(HisMemberRefund::getDelFlag, 0);
        return lqw;
    }

    private void fillOrderNumber(HisMemberRefundModel model) {
        if (Objects.nonNull(model.getOrderTreatId())) {
            String orderNumber = "";
            switch (model.getOrderType()) {
                case OrderTypeConstant.ORDER_REGISTRATION_TREAT:
                    orderNumber = registrationMapper.getOrderNumberById(model.getOrderTreatId());
                    break;
                case OrderTypeConstant.ORDER_COMMODITY_TREAT:
                    orderNumber = hisGoodsOrderMapper.getOrderNumberById(model.getOrderTreatId());
                    break;
                default:
                    break;
            }
            model.setOrderTreatNumber(orderNumber);
        }
    }

    private void fillOrderNumber(HisMemberRefund model) {
        if (Objects.nonNull(model.getOrderTreatId())) {
            String orderNumber = "";
            switch (model.getOrderType()) {
                case OrderTypeConstant.ORDER_REGISTRATION_TREAT:
                    orderNumber = registrationMapper.getOrderNumberById(model.getOrderTreatId().toString());
                    break;
                case OrderTypeConstant.ORDER_COMMODITY_TREAT:
                    orderNumber = hisGoodsOrderMapper.getOrderNumberById(model.getOrderTreatId().toString());
                    break;
                default:
                    break;
            }
            model.setOrderTreatNumber(orderNumber);
        }
    }
}
